package com.smartwebx.fr.util;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.smartwebx.fr.bean.SimpleUseArgConf;
import com.smartwebx.fr.service.StoredProcService;

@Component
public class InitLoadingSites implements ApplicationListener<ApplicationEvent> {
	@Autowired
	private StoredProcService storedProcService;

	private static Map<String, String> siteMaps = Maps.newHashMap();

	@Override
	public void onApplicationEvent(ApplicationEvent arg0) {

		if (siteMaps.isEmpty()) {
			// 添加单站信息
			String querySql = "select stcd,stnm from JSSQ.ST_STBPRP_B where stcd is not null and stnm is not null";
			List<SimpleUseArgConf> useArgs = Lists.newArrayList();
			List<List<String>> results = storedProcService.getResultBySqlAndPageParam(querySql, useArgs, "table",
					false);

			for (List<String> line : results) {
				String id = line.get(0);
				String text = line.get(1);
				siteMaps.put(id, text);
			}

			// 添加站群单站信息
			querySql = "select * from jssq.JS_PFQN_B where astcd is not null and astnm is not null";
			results = storedProcService.getResultBySqlAndPageParam(querySql, useArgs, "table", false);
			for (List<String> line : results) {
				String id = line.get(1);
				String text = line.get(2);
				siteMaps.put(id, text);
			}
		}
	}

	public static Map<String, String> getAllSites() {
		return siteMaps;
	}
}
